﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Trirand.Web.Mvc;
using OneDollarStore.Areas.Admin.Models;

namespace OneDollarStore.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class DesktopController : Controller
    {
        StoreModelEntities storeDB = new StoreModelEntities();

        public ViewResult Index()
        {
            DesktopJqGridModel gridModel = new DesktopJqGridModel();
            // Set grid's parameters
            SetUpGrid(gridModel.DesktopGrid);
            return View(gridModel);
        }

        public JsonResult SearchGridDataRequested()
        {
            StoreModelEntities storeDB = new StoreModelEntities();
            DesktopJqGridModel gridModel = new DesktopJqGridModel();
            List<DesktopModel> items = (from p in storeDB.SerialProducts
                                       where p.Category.Name == "Desktop"
                                       orderby p.Name
                                       select new DesktopModel
                                       {
                                           DesktopID = p.SerialProductID,
                                           Name = p.Name,
                                           Description = p.Description,
                                           Image = p.Image,
                                           Warranty = p.Warranty,
                                           DealerPrice = p.DealerPrice,
                                           RetailPrice = p.RetailPrice,
                                           PromotionPrice = p.PromotionPrice,
                                           PromotionStartDate = p.PromotionStartDate,
                                           PromotionEndDate = p.PromotionEndDate,
                                           Quantity = p.Quantity,
                                           Highlight = p.Highlight,
                                           Category = p.Category.CategoryID,
                                           Manufacturer = p.Manufacturer.ManufacturerID,
                                           Mainboard = p.Desktop.Mainboard,
                                           CPU = p.Desktop.CPU,
                                           RAM = p.Desktop.RAM,
                                           HDD = p.Desktop.HDD,
                                           VGA = p.Desktop.VGA,
                                           CDDVD = p.Desktop.CDDVD,
                                           Network = p.Desktop.Network,
                                           OperatingSystem = p.Desktop.OperatingSystem
                                       }).ToList();
            return gridModel.DesktopGrid.DataBind(items.AsQueryable());
        }

        public void EditRows()
        {
            StoreModelEntities storeDB = new StoreModelEntities();
            DesktopJqGridModel gridModel = new DesktopJqGridModel();
            var editData = gridModel.DesktopGrid.GetEditData();
            if (gridModel.DesktopGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                int CategoryID = Convert.ToInt32(editData.RowData["Category"]);
                int ManufacturerID = Convert.ToInt32(editData.RowData["Manufacturer"]);
                // Add to SerialProduct table
                SerialProduct product = new SerialProduct();
                product.Name = editData.RowData["Name"];
                product.Description = editData.RowData["Description"];
                product.Image = editData.RowData["Image"];
                product.Warranty = Convert.ToDouble(editData.RowData["Warranty"]);
                product.DealerPrice = Convert.ToDecimal(editData.RowData["DealerPrice"]);
                product.RetailPrice = Convert.ToDecimal(editData.RowData["RetailPrice"]);
                product.PromotionPrice = Convert.ToDecimal(editData.RowData["PromotionPrice"]);
                product.PromotionStartDate = Convert.ToDateTime(editData.RowData["PromotionStartDate"]);
                product.PromotionEndDate = Convert.ToDateTime(editData.RowData["PromotionEndDate"]);
                product.Quantity = Convert.ToInt32(editData.RowData["Quantity"]);
                product.Highlight = (editData.RowData["Highlight"] == "on") ? true : false;
                product.Category = storeDB.Categories.First(d => d.CategoryID == CategoryID);
                product.Manufacturer = storeDB.Manufacturers.First(d => d.ManufacturerID == ManufacturerID);
                storeDB.AddToSerialProducts(product);
                // Add to Desktop table
                Desktop desktop = new Desktop();
                desktop.Mainboard = editData.RowData["Mainboard"];
                desktop.CPU = editData.RowData["CPU"];
                desktop.RAM = editData.RowData["RAM"];
                desktop.HDD = editData.RowData["HDD"];
                desktop.VGA = editData.RowData["VGA"];
                desktop.CDDVD = editData.RowData["CDDVD"];
                desktop.Network = editData.RowData["Network"];
                desktop.OperatingSystem = editData.RowData["OperatingSystem"];
                storeDB.AddToDesktops(desktop);
                storeDB.SaveChanges();
            }
            if (gridModel.DesktopGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                int DesktopID = Convert.ToInt32(editData.RowData["DesktopID"]);
                int ManufacturerID = Convert.ToInt32(editData.RowData["Manufacturer"]);
                int CategoryID = Convert.ToInt32(editData.RowData["Category"]);
                SerialProduct product = (from p in storeDB.SerialProducts.Include("Category").Include("Manufacturer")
                                         where p.SerialProductID == DesktopID
                                         select p).First();
                product.Name = editData.RowData["Name"];
                product.Description = editData.RowData["Description"];
                product.Image = editData.RowData["Image"];
                product.Warranty = Convert.ToDouble(editData.RowData["Warranty"]);
                product.DealerPrice = Convert.ToDecimal(editData.RowData["DealerPrice"]);
                product.RetailPrice = Convert.ToDecimal(editData.RowData["RetailPrice"]);
                product.PromotionPrice = Convert.ToDecimal(editData.RowData["PromotionPrice"]);
                product.PromotionStartDate = Convert.ToDateTime(editData.RowData["PromotionStartDate"]);
                product.PromotionEndDate = Convert.ToDateTime(editData.RowData["PromotionEndDate"]);
                product.Quantity = Convert.ToInt32(editData.RowData["Quantity"]);
                product.Highlight = (editData.RowData["Highlight"] == "on") ? true : false;
                product.Category = storeDB.Categories.First(d => d.CategoryID == CategoryID);
                product.Manufacturer = storeDB.Manufacturers.First(d => d.ManufacturerID == ManufacturerID);
                Desktop desktop = (from l in storeDB.Desktops
                                 where l.DesktopID == DesktopID
                                 select l).First();
                desktop.Mainboard = editData.RowData["Mainboard"];
                desktop.CPU = editData.RowData["CPU"];
                desktop.RAM = editData.RowData["RAM"];
                desktop.HDD = editData.RowData["HDD"];
                desktop.VGA = editData.RowData["VGA"];
                desktop.CDDVD = editData.RowData["CDDVD"];
                desktop.Network = editData.RowData["Network"];
                desktop.OperatingSystem = editData.RowData["OperatingSystem"];
                storeDB.SaveChanges();
            }
            if (gridModel.DesktopGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                int DesktopID = Convert.ToInt32(editData.RowData["DesktopID"]);
                Desktop desktop = (from l in storeDB.Desktops
                                 where l.DesktopID == DesktopID
                                 select l).First();
                storeDB.DeleteObject(desktop);
                SerialProduct product = (from p in storeDB.SerialProducts
                                         where p.SerialProductID == DesktopID
                                         select p).First();
                storeDB.DeleteObject(product);
                storeDB.SaveChanges();
            }
        }

        private void SetUpGrid(JQGrid desktopGrid)
        {
            desktopGrid.ID = "DesktopGrid";
            desktopGrid.DataUrl = Url.Action("SearchGridDataRequested");
            desktopGrid.EditUrl = Url.Action("EditRows");
            desktopGrid.ToolBarSettings.ShowAddButton = true;
            desktopGrid.ToolBarSettings.ShowEditButton = true;
            desktopGrid.ToolBarSettings.ShowDeleteButton = true;
            desktopGrid.ToolBarSettings.ShowSearchButton = true;
            desktopGrid.ToolBarSettings.ShowRefreshButton = true;
            desktopGrid.AddDialogSettings.CloseAfterAdding = true;
            desktopGrid.EditDialogSettings.CloseAfterEditing = true;
            // Setup the dropdown values for the category and manufacturer editing dropdown
            SetUpCategoryEditDropDown(desktopGrid);
            SetUpManufacturerEditDropDown(desktopGrid);
        }

        private void SetUpCategoryEditDropDown(JQGrid desktopGrid)
        {
            JQGridColumn categoryColumn = desktopGrid.Columns.Find(c => c.DataField == "Category");
            // Populate the search dropdown only on initial request, in order to optimize performance
            if (desktopGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                StoreModelEntities storeDB = new StoreModelEntities();
                var editList = (from category in storeDB.Categories
                                select category);
                List<SelectListItem> list = new List<SelectListItem>();
                foreach (var item in editList)
                {
                    list.Add(new SelectListItem
                    {
                        Text = item.Name,
                        Value = item.CategoryID.ToString()
                    });
                }
                categoryColumn.EditList = list;
            }
        }

        private void SetUpManufacturerEditDropDown(JQGrid desktopGrid)
        {
            // Setup the grid search criteria for the columns
            JQGridColumn manufacturerColumn = desktopGrid.Columns.Find(c => c.DataField == "Manufacturer");
            // Populate the search dropdown only on initial request, in order to optimize performance
            if (desktopGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                StoreModelEntities storeDB = new StoreModelEntities();
                var editList = (from manufacturer in storeDB.Manufacturers
                                select manufacturer);
                List<SelectListItem> list = new List<SelectListItem>();
                foreach (var item in editList)
                {
                    list.Add(new SelectListItem
                    {
                        Text = item.Name,
                        Value = item.ManufacturerID.ToString()
                    });
                }
                manufacturerColumn.EditList = list;
            }
        }
    }
}